package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Appointment;
import com.example.demo.mapper.AppointmentMapper;
import com.example.demo.service.AppointmentService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class AppointmentServiceImpl extends ServiceImpl<AppointmentMapper, Appointment> implements AppointmentService {

    @Override
    public List<Appointment> getTodayAppointments(Integer doctorId) {
        LocalDate today = LocalDate.now();
        LocalDateTime start = today.atStartOfDay();
        LocalDateTime end = today.plusDays(1).atStartOfDay().minusSeconds(1);

        QueryWrapper<Appointment> wrapper = new QueryWrapper<>();
        wrapper.between("appointment_time", start, end);
        if (doctorId != null) {
            wrapper.eq("doctor_id", doctorId);
        }

        return baseMapper.selectList(wrapper);
    }

    @Override
    public boolean updateStatus(Integer id, String status) {
        Appointment appointment = new Appointment();
        appointment.setId(id);
        appointment.setStatus(status);
        return this.updateById(appointment);
    }

    @Override
    public List<Appointment> getAppointmentsByPatientName(String patientName) {
        QueryWrapper<Appointment> wrapper = new QueryWrapper<>();
        wrapper.eq("patient_name", patientName).orderByDesc("appointment_time");
        return baseMapper.selectList(wrapper);
    }
}
